package org.ltc.alliance.controller;

import org.ltc.alliance.constant.StatusConst;
import org.ltc.alliance.constant.WebConst;
import org.ltc.alliance.model.ClubDomain;
import org.ltc.alliance.model.ClubUserDomain;
import org.ltc.alliance.model.UserDomain;
import org.ltc.alliance.service.club.ClubService;
import org.ltc.alliance.service.clubuser.ClubUserService;
import org.ltc.alliance.service.power.PowerService;
import org.ltc.alliance.service.user.UserService;
import org.ltc.alliance.utils.APIResponse;
import org.ltc.alliance.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/club-user")
public class ClubUserController extends BaseController{

    @Autowired
    private ClubUserService clubUserService;
    @Autowired
    private PowerService powerService;
    @Autowired
    private ClubService clubService;
    @Autowired
    private UserService userService;

    @RequestMapping("/apply-join")
    @ResponseBody
    public APIResponse<?> applyToJoinClub(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        ClubUserDomain clubUser = new ClubUserDomain();
        clubUser.setCid(Integer.parseInt(clubId));
        clubUser.setUid(Integer.parseInt(uid));

        try {
            clubUserService.saveApplyInfo(clubUser);
        }
        catch (Exception e) {
            return APIResponse.fail(e.toString());
        }
        return APIResponse.success();
    }

    @RequestMapping("/apply-join-list")
    @ResponseBody
    public APIResponse<?> getApplyJoinList(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");

        try {
            List<ClubUserDomain> clubUsers = clubUserService.getListByCidAndStatus(Integer.parseInt(clubId),
                    StatusConst.ClubUser.APPLYING);
            return APIResponse.success(clubUsers);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
    }

    @RequestMapping("/joined-club-list")
    @ResponseBody
    public APIResponse<?> getJoinedClubList(
            HttpServletRequest request,
            HttpServletResponse response){

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        try {
            List<ClubUserDomain> clubUsers = clubUserService.getListByUidAndStatus(Integer.parseInt(uid),
                    StatusConst.ClubUser.NORMAL);
            List<ClubDomain> clubs = new ArrayList<>();
            for (ClubUserDomain clubUser : clubUsers) {
                clubs.add(clubService.getClubByCid(clubUser.getCid()));
            }
            return APIResponse.success(clubs);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
    }

    @RequestMapping("/review-apply-join")
    @ResponseBody
    public APIResponse<?> reviewApplyJoin(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");
        String applyPass = map.get("applyPass");
        String applyId = map.get("applyId");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        try {
            powerService.powerOverManager(Integer.parseInt(uid), Integer.parseInt(clubId));
            Boolean b = null;
            if (applyPass.equalsIgnoreCase("true") || applyPass.equalsIgnoreCase("false")){
                b = Boolean.valueOf(applyPass);
            }
            clubUserService.alterStatusByCidAndUid(Integer.parseInt(clubId), Integer.parseInt(applyId), b,
                    StatusConst.ClubUser.APPLYING);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
        return APIResponse.success();
    }

    @RequestMapping("/quit-club")
    @ResponseBody
    public APIResponse<?> quitClub(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");

        String uidString = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uidString = cookie.getValue();
            }
        }
        Integer uid = Integer.parseInt(uidString);
        try {
            clubUserService.quitClub(Integer.parseInt(clubId), uid);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
        return APIResponse.success();
    }

    @RequestMapping("/kick-user")
    @ResponseBody
    public APIResponse<?> kickUser(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");
        String receiver = map.get("receiver");

        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }

        try {
            powerService.powerOverManager(Integer.parseInt(uid), Integer.parseInt(clubId));
            clubUserService.kickUser(Integer.parseInt(uid), Integer.parseInt(receiver), Integer.parseInt(clubId));
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
        return APIResponse.success();
    }

    @RequestMapping("/club-member-list")
    @ResponseBody
    public APIResponse<?> getClubMemberList(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");
        try {
            List<ClubUserDomain> clubUsers = clubUserService.getListByCidAndStatus(Integer.parseInt(clubId),
                    StatusConst.ClubUser.NORMAL);
            List<UserDomain> users = new ArrayList<>();
            for (ClubUserDomain clubUser : clubUsers) {
                users.add(userService.getUserBriefInfoById(clubUser.getUid()));
            }
            return APIResponse.success(users);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
    }

}
